﻿using Extension.Ext;
using Extension.Script;
using PatcherYRpp;
using System;

namespace DpLib.Scripts.Heros
{

    [Serializable]
    public class AthenaScript : TechnoScriptable
    {
        public AthenaScript(TechnoExt owner) : base(owner)
        {

        }


        //光束颜色
        static ColorStruct innerColor = new ColorStruct(255, 0, 0);
        static ColorStruct outerColor = new ColorStruct(0, 0, 0);
        static ColorStruct outerSpread = new ColorStruct(0, 0, 0);

        private bool isActived = false;

        //爆炸使用的武器
        static Pointer<WeaponTypeClass> weapon => WeaponTypeClass.ABSTRACTTYPE_ARRAY.Find("AthenaAirStrike");
        static Pointer<WeaponTypeClass> eweapon => WeaponTypeClass.ABSTRACTTYPE_ARRAY.Find("AthenaAirStrikeE");


        private int maxHealth = 650;

        /// <summary>
        /// 扫描的间隔，为0退出当前攻击
        /// </summary>
        private int scaningDelay = 0;

        private bool isWorking = false;

        private double angle = 0d;

        private double radius;

        private double currentAngle;

        private int coolDown = 0;

        private CoordStruct start;
        private CoordStruct target;

        private int maxAttackCount = 300;
        private int currentAttackCount = 0;

        public override void OnUpdate()
        {
            if (!isActived && coolDown > 0)
            {
                coolDown--;
            }

            if (scaningDelay > 0)
            {
                if (isActived)
                {
                    if (currentAngle > 0)
                    {
                        var posL = new CoordStruct(start.X + (int)(radius * Math.Round(Math.Cos((angle + currentAngle) * Math.PI / 180), 5)), start.Y + (int)(radius * Math.Round(Math.Sin((angle + currentAngle) * Math.PI / 180), 5)), target.Z);
                        Pointer<LaserDrawClass> pLaser1 = YRMemory.Create<LaserDrawClass>(start, posL, innerColor, outerColor, outerSpread, 5);
                        pLaser1.Ref.Thickness = 10;
                        pLaser1.Ref.IsHouseColor = false;

                        var posR = new CoordStruct(start.X + (int)(radius * Math.Round(Math.Cos((angle - currentAngle) * Math.PI / 180), 5)), start.Y + (int)(radius * Math.Round(Math.Sin((angle - currentAngle) * Math.PI / 180), 5)), target.Z);
                        Pointer<LaserDrawClass> pLaser2 = YRMemory.Create<LaserDrawClass>(start, posR, innerColor, outerColor, outerSpread, 5);
                        pLaser2.Ref.Thickness = 10;
                        pLaser2.Ref.IsHouseColor = false;

                        currentAngle -= 0.3;
                    }
                    else
                    {
                        FireWeapon(target);
                        Init();
                        ResetCoolDown();
                    }
                }

            }
            else
            {
                Init();
            }

            if (scaningDelay > 0)
            {
                scaningDelay--;
            }
        }


        private void FireWeapon(CoordStruct target)
        {
            if (MapClass.Instance.TryGetCellAt(target, out var pCell))
            {
                var myweapon = Owner.OwnerObject.Ref.Veterancy.IsElite() ? weapon : eweapon;
                var bullet = myweapon.Ref.Projectile;
                var pBullet = bullet.Ref.CreateBullet(pCell.Convert<AbstractClass>(), Owner.OwnerObject, myweapon.Ref.Damage, myweapon.Ref.Warhead, myweapon.Ref.Speed, myweapon.Ref.Bright);

                //这里(0,0,2000)是相对于目标点的位置，即从目标点+2000高度的位置开始发射抛射体
                pBullet.Ref.MoveTo(target + new CoordStruct(0, 0, 2000), new BulletVelocity(0, 0, 0));
                pBullet.Ref.SetTarget(pCell.Convert<AbstractClass>());
            }
        }

        private void ResetCoolDown()
        {
            coolDown = 250;
        }

        public override void OnFire(Pointer<AbstractClass> pTarget, int weaponIndex)
        {
            var pTechno = Owner.OwnerObject;
     
            if (weaponIndex == 0 && coolDown <= 0)
            {
                scaningDelay = 20;
                if (isWorking == false)
                {
                    Init();
                    isWorking = true;
                    var start = pTechno.Ref.Base.Base.GetCoords() + new CoordStruct(0, 0, 120);
                    var target = pTarget.Ref.GetCoords();

                    //半径
                    var radius = new CoordStruct(start.X, start.Y, 0).DistanceFrom(new CoordStruct(target.X, target.Y, 0));

                    var angle = 0d;

                    //判断是否与水平线重合
                    if (start.Y == target.Y)
                    {
                        if (start.X > target.X)
                        {
                            angle = 180;
                        }
                        else
                        {
                            angle = 0;
                        }
                    }
                    else if (start.X == target.X)
                    {
                        if (start.Y < target.Y)
                        {
                            angle = 90;
                        }
                        else
                        {
                            angle = 270;
                        }
                    }
                    else
                    {
                        var k = (double)((double)target.Y - start.Y) / ((double)target.X - start.X);
                        angle = Math.Atan(Math.Abs(k)) * 180 / Math.PI;


                        //第一象限
                        if (start.X < target.X && start.Y < target.Y)
                        {

                        }
                        else if (start.X < target.X && start.Y > target.Y)
                        {
                            angle = 360 - angle;
                        }
                        else if (start.X > target.X && start.Y > target.Y)
                        {
                            //第三象限
                            angle = 180 + angle;
                        }
                        else
                        {
                            //第四象限
                            angle = 180 - angle;
                        }

                    }

                    this.start = start;
                    this.target = target;
                    this.angle = angle;
                    this.radius = radius;
                    currentAngle = 30;

                    isActived = true;

                }


            }
        }


        private void Init()
        {
            angle = 0;
            currentAngle = 0;
            radius = 0;
            isWorking = false;
            isActived = false;
            currentAttackCount = 0;
        }


    }
}